import { useEffect } from 'react';
import { message } from 'antd';

import pageTabUtil from '@/utils/pageTabUtil';

/**
 * 如果遇到流程申请页继续回退
 * @param location
 * @param unBlock
 */
const continueBackWhenIsApply = (location, unBlock) => {
    if (location.pathname === '/personal/portal/apply') {
        const historyList = pageTabUtil.history[pageTabUtil.getCurrentKey()];
        if (historyList.length >= 2 && historyList[historyList.length - 2].pathname === '/personal/portal/apply') {
            window.setTimeout(() => {
                message.destroy();
                pageTabUtil.goTabHistory(pageTabUtil.getCurrentKey(), -1);
            }, 250);
        } else {
            unBlock();
        }
    } else {
        unBlock();
    }
};

const useHistoryBlock = (history, callback) => {
    useEffect(() => {
        const unBlock = history.block((...args) => {
            if (callback) {
                return callback(...args, unBlock);
            }
            return continueBackWhenIsApply(args[0], unBlock);
        });
        return unBlock;
    }, []);
};

export default useHistoryBlock;
